package com.mxpio.erp.oa.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.oa.entity.SignatureApplication;
import com.mxpio.erp.common.oa.service.SignatureApplicationService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Tag(name = "SignatureApplicationController", description = "签呈申请")
@RestController
@RequestMapping("/erp/oa/ssa/")
public class SignatureApplicationController {

    @Autowired
    private SignatureApplicationService service;

    @GetMapping("list")
    @Operation(summary = "签呈申请列表", description = "签呈申请列表（无分页）", method = "GET")
    public Result<List<SignatureApplication>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<SignatureApplication> list = service.list(SignatureApplication.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "签呈申请列表", description = "签呈申请列表（分页）", method = "GET")
    public Result<Page<SignatureApplication>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<SignatureApplication> pageResult = service.listPage(SignatureApplication.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据ID获取签呈申请", description = "根据ID获取签呈申请", method = "GET")
    public Result<SignatureApplication> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        SignatureApplication reimbursement = service.getById(SignatureApplication.class, bizNo);
        return Result.OK(reimbursement);
    }

    @RequestMapping("add")
    @Operation(summary = "保存报销表", description = "保存报销表")
    public Result<SignatureApplication> add(@RequestBody SignatureApplication reimbursement) {
        service.save(reimbursement);
        return  Result.OK();
    }

    @PutMapping("edit")
    @Operation(summary = "编辑", description = "编辑", method = "PUT")
    public Result<SignatureApplication> edit(@RequestBody SignatureApplication reimbursement) {
        SignatureApplication application = service.getById(SignatureApplication.class,reimbursement.getBizNo());
        if (!application.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())){
            return Result.error("在开立状态下才可修改");
        }
        service.update(reimbursement);
        return Result.OK(reimbursement);
    }

    /**
     * 用于审批工工作流时进行-编辑
     * @param reimbursement
     * @return
     */
    @PutMapping("edit/workflows")
    @Operation(summary = "工作流审批时-编辑", description = "工作流审批时-编辑", method = "PUT")
    public Result<SignatureApplication> editWorkflows(@RequestBody SignatureApplication reimbursement) {
        SignatureApplication byId = service.getById(SignatureApplication.class, reimbursement.getBizNo());
        byId.setReviewOpinions(reimbursement.getReviewOpinions());//审批意见
        service.update(byId);
        return Result.OK(byId);
    }

    @DeleteMapping("remove/{bizNos}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<SignatureApplication> remove(@PathVariable(name = "bizNos", required = true) String bizNos) {
        String[] id = bizNos.split(",");
        for(String key : id){
            service.delete(SignatureApplication.class, key);
        }
        return Result.OK();
    }

    /**
     * 编辑审批状态
     * @param reimbursement
     * @return
     */
    @PutMapping("edit/bpmn/")
    @Operation(summary = "编辑审批状态", description = "编辑审批状态", method = "PUT")
    public Result<SignatureApplication> editBpmn(@RequestBody SignatureApplication reimbursement) {
        service.update(reimbursement);
        return Result.OK(reimbursement);
    }

    @RequestMapping("submit/{key}")
    @Operation(summary = "提交申请-工作流",description = "提交申请-工作流")
    public Result<?> change(@PathVariable(name = "key") String key,
                            @RequestParam(value="businessKey", required = false) String businessKey,
                            @RequestBody Map<String, Object> properties) {
        return service.submit(key, SecurityUtils.getLoginUsername(), businessKey, properties);
    }

    @RequestMapping("submit/Ok")
    @Operation(summary = "提交申请-工作流审核",description = "提交申请-工作流审核")
    public Result<?> submitOK(@RequestBody SignatureApplication reimbursement) {
        return service.submitOK(reimbursement);
    }
}
